package com.sec.android.app.myfiles.decorator.tablet;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.decorator.AbsAdapterImpDecorator;
import com.sec.android.app.myfiles.feature.MultiWindowMgr;
import com.sec.android.app.myfiles.fragment.filelist.FileListAdapter;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.download.DownloadFileRecord;
import com.sec.android.app.myfiles.module.local.recent.RecentFileRecord;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.listview.ColumnListViewImp;
import com.sec.android.app.myfiles.widget.listview.FileListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;

import java.util.Locale;

/**
 * Created by jaebae on 2016-01-21.
 */
public class TabletColumnListAdapterDecorator extends AbsAdapterImpDecorator {
    private int mColumnWidth[];
    private MultiWindowMgr mMultiWindow;

    @Override
    protected void _bindCreateFolderView(ListViewHolder vh, View v) {
        // do nothing
    }

    public TabletColumnListAdapterDecorator(Context context, FileListAdapter adapter, MultiWindowMgr multiWindowMgr) {
        super(context, adapter);
        mColumnWidth = new int[ColumnListViewImp.ColumnInfo.MAX];
        mMultiWindow = multiWindowMgr;
        setColumnWidth();
    }

    @Override
    protected void _createViewHolder(View v, ListViewHolder vh) {
        vh.addView(FileListViewHolder.NAME_COLUMN, v.findViewById(R.id.column_name));
        vh.addView(FileListViewHolder.TYPE, v.findViewById(R.id.file_type));

        setColumnViewWidth(vh);
    }

    @Override
    protected void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        TextView textType = vh.getView(FileListViewHolder.TYPE, TextView.class);
        if (textType != null) {
            textType.setVisibility(View.VISIBLE);
            if (record instanceof DownloadFileRecord && record.getStorageType() == FileRecord.StorageType.Downloads) {
                textType.setText(((DownloadFileRecord) record).getDescription());
            } else {
                if (record.getFileType() == FileType.FOLDER) {
                    textType.setText(R.string.folder);
                } else {
                    if (record.getExt() != null) {
                        textType.setText(record.getExt().toUpperCase(Locale.getDefault()));
                    }
                }
            }
        }
        TextView textDate = vh.getView(FileListViewHolder.DATE, TextView.class);
        if (textDate != null) {
            if (record instanceof RecentFileRecord && record.getStorageType() == FileRecord.StorageType.Recent) {
                textDate.setText(UiUtils.makeTimeString(mContext, ((RecentFileRecord) record).getLastAccessedTime()));
            }
        }
        setColumnViewWidth(vh);
    }

    @Override
    protected void _bindProgressSync(ListViewHolder vh, View v, FileRecord record, boolean isShow) {
        // do nothing
    }

    private void setColumnViewWidth(ListViewHolder vh) {
        setViewWidth(vh.getView(FileListViewHolder.NAME_COLUMN), mColumnWidth[ColumnListViewImp.ColumnInfo.NAME]);
        setViewWidth(vh.getView(FileListViewHolder.DATE), mColumnWidth[ColumnListViewImp.ColumnInfo.TIME]);
        setViewWidth(vh.getView(FileListViewHolder.TYPE), mColumnWidth[ColumnListViewImp.ColumnInfo.TYPE]);
        setViewWidth(vh.getView(FileListViewHolder.SIZE), mColumnWidth[ColumnListViewImp.ColumnInfo.SIZE]);
    }

    private void setViewWidth(View v, int width) {
        if (v != null) {
            ViewGroup.LayoutParams params = v.getLayoutParams();
            params.width = width;
            v.setLayoutParams(params);
        }
    }


    public void setColumnWidth(int columnIndex, int width) {
        mColumnWidth[columnIndex] = width;
    }

    private void setColumnWidth() {
        for (int i = 0; i < ColumnListViewImp.ColumnInfo.MAX; i++) {
            int width = PreferenceUtils.getColumnListWidth(mContext, mMultiWindow.getWindowState(), i);
            setColumnWidth(i, width);
        }
    }
}
